<?php
if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

$ac = htmlspecialchars($_G['gp_action']);
$config = $_G['cache']['plugin']['longdu_vote'];
$usergroup = unserialize($config['usergroup']);

if ($ac == 'single'){
	require_once libfile('function/post');
	$attachlist = getattach($_GET['pid'], intval($_GET['posttime']), $_GET['aids']);
	$imagelist = $attachlist['imgattachs']['unused'];
	include template('common/header_ajax');
	include template('longdu_threadpic:ajax_imagelist');
	include template('common/footer_ajax');
	dexit();
}elseif($ac == 'plupload'){
	$upload = new discuz_upload();
	$_FILES["file"]['name'] = addslashes(diconv(urldecode($_FILES["file"]['name']), 'UTF-8'));
	$upload->init($_FILES['file'], 'forum');
	$attach = $upload->attach;
	$upload->save();
	$errorcode = 0;
	if($upload->error()) {
		$errorcode = 4;
	} else {
		if($attach['isimage']) {
			require_once libfile('class/image');
			$image = new image();
			$thumbimgwidth = 100;
			$thumbimgheight = 98;
			$attach['thumb'] = $image->Thumb($attach['target'], '', $thumbimgwidth, $thumbimgheight, 2);
			$image->Watermark($attach['target'], '', 'forum');
			$imginfo = @getimagesize($attach['target']);
			if($imginfo !== FALSE) {
				$attach['width'] = $imginfo[0];
			}
		}
		if(getglobal('setting/ftp/on') && ((!$_G['setting']['ftp']['allowedexts'] && !$_G['setting']['ftp']['disallowedexts']) || ($_G['setting']['ftp']['allowedexts'] && in_array($attach['ext'], $_G['setting']['ftp']['allowedexts'])) || ($_G['setting']['ftp']['disallowedexts'] && !in_array($attach['ext'], $_G['setting']['ftp']['disallowedexts']))) && (!$_G['setting']['ftp']['minsize'] || $attach['size'] >= $_G['setting']['ftp']['minsize'] * 1024)) {
			if(ftpcmd('upload', 'forum/'.$attach['attachment']) && (!$attach['thumb'] || ftpcmd('upload', 'forum/'.getimgthumbname($attach['attachment'])))) {
				@unlink($_G['setting']['attachdir'].'/forum/'.$attach['attachment']);
				@unlink($_G['setting']['attachdir'].'/forum/'.getimgthumbname($attach['attachment']));
				$attach['remote'] = 1;
			} else {
				if(getglobal('setting/ftp/mirror')) {
					@unlink($attach['target']);
					@unlink(getimgthumbname($attach['target']));
					$errorcode = 5;
				}
			}
		}
	}
	if(!$errorcode) {
		$setarr = array(
				'uid' => $_G['uid'],
				'filename' => $attach['name'],
				'attachment' => $attach['attachment'],
				'filesize' => $attach['size'],
				'thumb' => $attach['thumb'],
				'remote' => $attach['remote'],
				'dateline' => $_G['timestamp'],
				'fid' => intval($_GET['fid']),
				'width' => $attach['width']
		);
		$attach['attachid'] = C::t('#longdu_threadpic#threadpic_plugin')->insert($setarr, true);
		require_once libfile('function/home');
		$smallimg = pic_get($attach['attachment'], 'forum', $attach['thumb'], $attach['remote']);
		$bigimg = pic_get($attach['attachment'], 'forum', 0, $attach['remote']);
		echo '{"aid":"'.$attach[attachid].'","path":"'.$smallimg.'","attachurl":"'.$bigimg.'"}';
		exit();
	} else {
		echo "{\"aid\":0, \"errorcode\":$errorcode}";
	}
}elseif($ac == 'deleteattach') {
	$aid = intval($_GET['aid']);
	if($aid) {
		$image = C::t('#longdu_threadpic#threadpic_plugin')->fetch($aid);
	}
	if($image['uid'] == $_G['uid']) {
		C::t('#longdu_threadpic#threadpic_plugin')->delete($aid);
		@unlink($_G['setting']['attachdir'].'/forum/'.$image['attachment']);
		@unlink($_G['setting']['attachdir'].'/forum/'.getimgthumbname($image['attachment']));
	}
	exit(1);
}
?>